<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--列表显示-->
<table id="userTable" lay-filter="userFilter"></table>

<!--行编辑工具栏-->
<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!--头部工具栏-->
<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    </div>
</script>

<!--客户添加form-->
<form id="userAddForm" class="layui-form" style="display:none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">部门ID</label>
        <div class="layui-input-block">
            <input type="text" name="deptId" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">登录账号</label>
        <div class="layui-input-block">
            <input type="text" name="loginName" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户昵称</label>
        <div class="layui-input-block">
            <input type="text" name="userName" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号码</label>
        <div class="layui-input-block">
            <input type="text" name="phonenumber" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="0" title="男" checked>
            <input type="radio" name="sex" value="1" title="女" >
            <input type="radio" name="sex" value="2" title="未知" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <input type="hidden" name="avatar" id="avatar">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="uploadFile">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">帐号状态</label>
        <div class="layui-input-block">
            <input type="text" name="status" placeholder="请输入"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">删除标志</label>
        <div class="layui-input-block">
            <input type="text" name="delFlag" placeholder="请输入"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">标记</label>
        <div class="layui-input-block">
            <input type="text" name="remark" placeholder="请输入"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="userAddFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--部门更新form-->
<form id="userUpdateForm" class="layui-form" lay-filter="userUpdateFilter" style="display:none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input type="hidden" name="userId">

    <div class="layui-form-item">
        <label class="layui-form-label">部门ID</label>
        <div class="layui-input-block">
            <input type="text" name="deptId" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">登录账号</label>
        <div class="layui-input-block">
            <input type="text" name="loginName" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户昵称</label>
        <div class="layui-input-block">
            <input type="text" name="userName" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号码</label>
        <div class="layui-input-block">
            <input type="text" name="phonenumber" placeholder="请输入" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="0" title="男" checked>
            <input type="radio" name="sex" value="1" title="女" >
            <input type="radio" name="sex" value="2" title="未知" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="启动" checked>
            <input type="radio" name="status" value="1" title="禁用" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">删除标志</label>
        <div class="layui-input-block">
            <input type="text" name="delFlag" placeholder="请输入"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">标记</label>
        <div class="layui-input-block">
            <input type="text" name="remark" placeholder="请输入"  autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="userUpdateFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--js代码-->
<script src="/layui/layui.js"></script>

<script>
    layui.use(['layer','table','jquery','form','upload'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#uploadFile' //绑定元素
            ,url: '/user/uploadHeadPic' //上传接口
            ,auto:true // 选择文件后自动上传
            ,field:"headPic"
            ,done: function(res){
                 layer.msg(JSON.stringify(res));
                //上传完毕回调
                if(res.code==0){
                    $("#avatar").val(res.data.filePath);
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });


        //客户列表js
        var userTable = table.render({
            elem: '#userTable'
            ,toolbar: '#toolbarHead'
            ,url: '/user/queryAll' //数据接口
            ,page: true //开启分页
            ,cellMinWidth:80
            ,cols: [
                [ //表头
                    {type: 'checkbox',fixed: 'left'}
                    ,{field: 'userId', title: '用户ID',  sort: true, fixed: 'left'}
                    ,{field: 'deptId', title: '部门ID', sort:true}
                    ,{field: 'loginName', title: '登录账号',  sort: true}
                    ,{field: 'userName', title: '用户昵称',  sort: true}
                    ,{field: 'email', title: '用户邮箱',  sort: true}
                    ,{field: 'phonenumber', title: '手机号码',  sort: true}
                    ,{field: 'sex', title: '性别',
                    templet: function(d){
                        return d.sex==0?'男':d.sex==1?'女':'未知';
                        /*if(d.sex==0){

                        }*/
                    },sort: true}
                    ,{field: 'avatar', title: '头像'},
                    ,{field: 'password', title: '密码',  sort: true}
                    ,{field: 'salt', title: '盐加密',  sort: true}
                    ,{field: 'status', title: '帐号状态',
                    templet: function(d){
                    return d.status=0?'正常':'停用';
                    /*if(d.sex==0){

                    }*/
                     }, sort: true}
                    ,{field: 'delFlag', title: '删除标志',
                    templet: function(d){
                        return d.delFlag=0?'存在':'删除';
                        /*if(d.sex==0){

                        }*/
                    }, sort: true}
                    ,{field: 'createBy', title: '创建人'}
                    ,{field: 'createTime', title: '创建时间', sort: true}
                    ,{field: 'updateBy', title: '更新人',  sort: true}
                    ,{field: 'updateTime', title: '更新时间'}
                    ,{field: 'remark', title: '标记'}
                    ,{fixed: 'right',  align:'center', toolbar: '#barRowEdit'}
                ]
            ]
        });

        //行编辑工具栏监听事件
        table.on('tool(userFilter)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/user/deleteById",
                        type:"DELETE",
                        data:{userId:data.userId},
                        dataType:"json",
                        success:function(resp){
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                userTable.reload();
                            }
                        },
                        error:function(e){
                            layer.msg("请求失败");
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                form.val("userUpdateFilter",data);
                layer.open({
                    type: 1,
                    area:['400px','250px'],
                    content: $('#userUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });

        //头部工具栏监听事件
        table.on('toolbar(userFilter)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var checkData = checkStatus.data;
            switch(obj.event){
                case 'add':
                    // layer.msg('添加');
                    // $("#deptAddForm").reset();
                    document.getElementById("userAddForm").reset();
                    layer.open({
                        type: 1,
                        area:['400px','250px'],
                        content: $('#userAddForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    // layer.msg('删除');
                    // layer.msg(JSON.stringify(checkData));
                    if(checkData.length==0){
                        layer.msg('至少选择一行');
                        return;
                    }
                    layer.confirm('真的删除吗',function(index){
                        var userIds = "";
                        for(var i = 0; i < checkData.length; i++){
                            userIds+=checkData[i].userId+",";
                        }
                        userIds=userIds.substr(0,userIds.length-1);
                        layer.msg(userIds);
                        //调用批量删除
                        $.ajax({
                            url:"/user/deleteByIds",
                            type:"DELETE",
                            data:{userIds:userIds},
                            dataType:"json",
                            success:function(resp){
                                if(resp.code==0){
                                    layer.msg(resp.msg);
                                    userTable.reload();
                                }
                            },
                            error:function(e){
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'update':
                    // layer.msg('编辑');
                    if(checkData.length!=1){
                        layer.msg('只能选中一行');
                        return;
                    }
                    form.val("userUpdateFilter",checkData[0]);
                    layer.open({
                        type: 1,
                        area:['400px','250px'],
                        content: $('#userUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });

        //添加监听事件
        form.on('submit(userAddFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/user/insert",
                type:"POST",
                data:data.field,
                dataType:"json",
                success:function(resp){
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        layer.closeAll('page');
                        userTable.reload();
                    }
                },
                error:function(e){
                    layer.msg("请求失败");
                }
            });
            return false;
        });

        //更新监听事件
        form.on('submit(userUpdateFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/user/update",
                type:"PUT",
                data:data.field,
                dataType:"json",
                success:function(resp){
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        layer.closeAll('page');
                        userTable.reload();
                    }
                },
                error:function(e){
                    layer.msg("请求失败");
                }
            });
            return false;
        });
    });
</script>

</body>
</html>